import { onMounted, reactive } from 'vue';
import router from '@/router';
import { userUserLogin } from '@/api/login';
import { apiUserGetLoginUser } from '@/api/url/salesOrderManagement';
import notification from '@arco-design/web-vue/es/notification';

export default function importConfig() {
  const base = reactive<any>({
    isRemember: false,
    eyeOpen: true,
    formRef: null,
  });

  const images = [
    'https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/cd7a1aaea8e1c5e3d26fe2591e561798.png~tplv-uwbnlip3yd-webp.webp',
    'https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/6480dbc69be1b5de95010289787d64f1.png~tplv-uwbnlip3yd-webp.webp',
    'https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/0265a04fddbd77a19602a15d9d55d797.png~tplv-uwbnlip3yd-webp.webp',
  ];

  const form = reactive({
    userId: '',
    password: '',
  });

  const formRules = {
    userId: [{ required: true, message: '用户名不能为空' }],
    password: [{ required: true, message: '密码不能为空' }],
  };

  const changeEye = () => {
    base.eyeOpen = !base.eyeOpen;
  };

  const login = () => {
    base.formRef.validate((res) => {
      if (!res) {
        userUserLogin(form).then((res: any) => {
          if (res.code == '200') {
            localStorage.setItem('TOKEN', res.data.token);
            localStorage.setItem('userName', res.data.userName);
            localStorage.setItem('userId', res.data.userId);
            notification.success('登入成功');
            router.push('buyingRequisition');
          } else {
            notification.success(res.msg);
          }
        });
      }
    });
    // router.push('/order');
  };

  const register = () => {
    // console.log('注册');
  };

  const forgetPsw = () => {
    localStorage.removeItem('supplier');
  };

  onMounted(() => {
    const userInfo = localStorage.getItem('supplier');
    if (userInfo) {
      form.userId = JSON.parse(userInfo).userId;
      form.password = JSON.parse(userInfo).password;
    }
  });

  /**
   * handleSubmit
   */
  const handleSubmit = () => {
    console.log('111');
  };

  return {
    base,
    images,
    form,
    formRules,
    changeEye,
    login,
    register,
    forgetPsw,
    handleSubmit,
  };
}
